package cn.zgwj.pet.service.impl;

import cn.zgwj.basic.service.impl.BaseServiceImpl;
import cn.zgwj.pet.domain.Pet;
import cn.zgwj.pet.domain.PetDetail;
import cn.zgwj.pet.mapper.PetDetailMapper;
import cn.zgwj.pet.mapper.PetMapper;
import cn.zgwj.pet.service.IPetService;
import cn.zgwj.user.domain.Logininfo;
import cn.zgwj.user.domain.User;
import cn.zgwj.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PetServiceImpl  extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public void add(Pet pet) {
        //1.保存pet信息
        petMapper.save(pet);//返回自增ID
        //2.保存petDetail信息
        PetDetail detail = pet.getDetail();
        if(detail != null){
            detail.setPet_id(pet.getId());
            petDetailMapper.save(detail);
        }
    }

    @Override
    public void onSale(List<Long> ids) {
        Map<String,Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("onSaleTime", new Date());
        petMapper.onSale(map);

    }

    @Override
    public void offSale(List<Long> ids) {
        Map<String,Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("offSaleTime", new Date());
        petMapper.offSale(map);
    }

    @Override
    public Pet loadByIdDetail(Long id) {

        return petMapper.loadByIdDetail(id);
    }

    @Override
    public void adopt(Long id, Logininfo logininfo) {
        Pet pet = petMapper.loadById(id);
        //1.下架宠物
        pet.setState(0);
        pet.setOffsaletime(new Date());
        //2.绑定宠物领养人
        User user = userMapper.loadByLogininfoId(logininfo.getId());
        pet.setUser_id(user.getId());
        petMapper.update(pet);

        //3.@TODO 生成领养订单
        //4.@TODO 生成支付单并支付
    }
}
